package com.yilin.medical.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Process;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.yilin.medical.R;
import com.yilin.medical.app.Constant;
import com.yilin.medical.app.JxApplication;
import com.yilin.medical.broadcast.JxExitAppReceiver;
import com.yilin.medical.lsh.SimpleException;
import com.yilin.medical.utils.JxSPUitls;
import com.yilin.medical.views.ActTitle;
import com.yilin.medical.views.JxTipDialog;

public class BaseActivity extends FragmentActivity implements OnClickListener {
	protected Context mContext;
	public String tag;
	/** 全局的加载框对象，已经完成初始化. */
	public ProgressDialog mProgressDialog;
	private JxExitAppReceiver exitReceiver = new JxExitAppReceiver();
	private static final String EXIT_APP_ACTION = "com.micen.exit_app";
	protected ActTitle mTitle;
	protected SharedPreferences sp;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		sp = getSharedPreferences("BaseActivity", Context.MODE_PRIVATE);
		registerExitReceiver();

	}

	protected void setContentViewWithTitle(int id) {
		LayoutInflater inflater = getLayoutInflater();
		ViewGroup contentV = (ViewGroup) inflater.inflate(
				R.layout.base_act_title, null);
		inflater.inflate(id, contentV);
		setContentView(contentV);
		mTitle = (ActTitle) findViewById(R.id.act_title);
		mTitle.init(this);
	}

	public ActTitle getmTitle() {
		return mTitle;
	}

	public void startAcitivity(Class clazz) {
		Intent in = new Intent();
		in.setClass(this, clazz);
		Bundle bundle = new Bundle();
		bundle.putString("tag", clazz.getSimpleName());
		in.putExtras(bundle);
		startActivity(in);
		finish();
	}

	private void registerExitReceiver() {
		IntentFilter exitFilter = new IntentFilter();
		exitFilter.addAction(EXIT_APP_ACTION);
		registerReceiver(exitReceiver, exitFilter);

	}

	private void unRegisterExitReceiver() {
		unregisterReceiver(exitReceiver);
	}

	public void showUpdateDialog() {
		final Integer force = (Integer) JxSPUitls.get(mContext, "force", 0);
		String declare = (String) JxSPUitls.get(mContext, "declare", "版本更新");
		final String url = (String) JxSPUitls.get(mContext, "url", "");
		JxTipDialog dialog = new JxTipDialog(mContext);
		if (force == 1) {
			dialog.setStrCancel("退出");
		} else {
			dialog.setStrCancel("取消");
		}
		dialog.setMessage(declare);
		dialog.setStrOk("更新");
		dialog.setCancelable(false);
		final JxTipDialog finalDialog = dialog;
		dialog.setPositiveButton(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					if (!url.contains(".apk")) {
						Uri uri = Uri.parse("http://download.zhagen.com/");
						Intent it = new Intent(Intent.ACTION_VIEW, uri);
						startActivity(it);
					} else {

					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					finalDialog.dismiss();
				}

			}
		});

		dialog.setNegativeButton(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (force == 1) {
					moveTaskToBack(true);
					JxApplication.getInstance().exit();
					int nPid = android.os.Process.myPid();
					android.os.Process.killProcess(nPid);
					System.exit(0);
				} else {
					finalDialog.dismiss();
				}
			}
		});
		finalDialog.show();
	}

	public Dialog ExitDialog(Context context) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		// builder.setIcon(R.drawable.noti_icon);
		builder.setTitle("退出应用程序");
		builder.setMessage("确定要退出程序吗?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				moveTaskToBack(true);
				JxApplication.getInstance().exit();
				android.os.Process.killProcess(Process.myPid());
				System.exit(0);

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		return builder.create();
	}

	protected void onDestroy() {
		super.onDestroy();
		unRegisterExitReceiver();
	}

	/**
	 * 描述：显示进度框.
	 */
	public void showProgressDialog() {
		showProgressDialog(null);
	}

	/**
	 * 描述：显示进度框.
	 * 
	 * @param message
	 *            the message
	 */
	public void showProgressDialog(String message) {
		// 创建一个显示进度的Dialog
		// if(!TextUtils.isEmpty(message)){
		// mProgressMessage = message;
		// }
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
			// 设置点击屏幕Dialog不消失
			mProgressDialog.setCanceledOnTouchOutside(false);
		}
		mProgressDialog.setMessage(message);
		showDialog(Constant.DIALOGPROGRESS);
	}

	/**
	 * 描述：移除进度框.
	 */
	public void removeProgressDialog() {
		removeDialog(Constant.DIALOGPROGRESS);
	}

	@Override
	public void onClick(View v) {

	}

	protected void startAct(Class<? extends BaseActivity> clazz, boolean flag) {
		Intent i = new Intent(this, clazz);
		startActivity(i);
		if (flag) {
			finish();
		}
	}

	// 六大科室对应的tid "肿瘤", "心血管", "眼科", "消化", "内分泌", "更多"
	// public static final String DEPARTMENT_TYPE_TUMOR = "tumor";
	// public static final String DEPARTMENT_TYPE_CARDIOVASCULAR =
	// "cardiovasclar";
	// public static final String DEPARTMENT_TYPE_OPHTHALMOLOGY = "hthalmology";
	// public static final String DEPARTMENT_TYPE_DIGESTION = "digestion";
	// public static final String DEPARTMENT_TYPE_ENDOCRINE = "endocrine";
	// public static final String DEPARTMENT_TYPE_MORE = "more";

	public int getTidByString(String name) {

		if (name.equalsIgnoreCase(KeChengHomeActivity.DEPARTMENT_TYPE_TUMOR)) {
			return 25;
		} else if (name
				.equalsIgnoreCase(KeChengHomeActivity.DEPARTMENT_TYPE_CARDIOVASCULAR)) {
			return 26;
		} else if (name
				.equalsIgnoreCase(KeChengHomeActivity.DEPARTMENT_TYPE_OPHTHALMOLOGY)) {
			return 27;
		} else if (name
				.equalsIgnoreCase(KeChengHomeActivity.DEPARTMENT_TYPE_DIGESTION)) {
			return 28;
		} else if (name
				.equalsIgnoreCase(KeChengHomeActivity.DEPARTMENT_TYPE_ENDOCRINE)) {
			return 29;
		} else if (name
				.equalsIgnoreCase(KeChengHomeActivity.DEPARTMENT_TYPE_MORE)) {
			return 30;
		} else {
			throw new RuntimeException("没有对应的科室");
		}
	}
}